package com.ruoyi.common.sms.wlwx.client;

import com.ruoyi.common.sms.wlwx.util.MD5;
import com.ruoyi.common.sms.wlwx.vo.ResultMsg;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class SmsClientMain {

    public static String getReportResultMsg(String sendTarget, String content) throws Exception{
        ResultMsg resultMsg = new ResultMsg();

        // 发送短信地址，以下为示例地址，具体地址询问网关获取
        String url_send_sms = "https://smsapp.wlwx.com:8862/sendSms";
        // 用户账号，必填
        String cust_code = "560084";
        // 用户密码，必填
        String cust_pwd = "4UQ2VYT75J";
        // 短信内容，必填
//        String content = "happy new year!你好";
        // 接收号码，必填，同时发送给多个号码时,号码之间用英文半角逗号分隔
        String destMobiles = sendTarget;
        // 业务标识，选填，由客户自行填写不超过20位的数字
        String uid = "";
        // 长号码，选填
        String sp_code = "";
        // 是否需要状态报告
        String need_report = "yes";
        // 数字签名，签名内容根据 “短信内容+客户密码”进行MD5编码后获得
        String sign = content + cust_pwd;
        sign = MD5.getMD5(sign.getBytes("UTF-8"));
        String json_send_sms = "{\"cust_code\":\"" + cust_code + "\",\"sp_code\":\"" + sp_code + "\",\"content\":\"" + content +
                "\",\"destMobiles\":\"" + destMobiles + "\",\"uid\":\"" + uid + "\",\"need_report\":\"" + need_report + "\",\"sign\":\"" + sign + "\"}";
        URL url = new URL(url_send_sms);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        OutputStream out = httpURLConnection.getOutputStream();
        out.write(json_send_sms.getBytes("UTF-8"));
        System.out.println("send msg:\t"+json_send_sms);
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;
        try {
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }
            resultMsg.setSuccess(true);
            resultMsg.setData(resultBuffer.toString());
            System.out.println("recv msg:\t"+resultBuffer.toString());
        } catch (Exception e) {
            throw e;
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
            httpURLConnection.disconnect();
        }
        return resultBuffer.toString();
    }
}
